package com.getpebble.android.common.core.trace;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.OnAccountsUpdateListener;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import com.getpebble.android.common.core.async.PblAsyncTask;
import java.lang.ref.WeakReference;
import java.util.Map;

public class UserObserver implements OnAccountsUpdateListener {
    private WeakReference<Context> mContext;

    public UserObserver(Context context) {
        if (context == null) {
            throw new IllegalArgumentException("Context must not be null");
        }
        this.mContext = new WeakReference(context);
        register();
        AccountManager accountManager = AccountManager.get((Context) this.mContext.get());
        if (accountManager != null) {
            Account[] myAccounts = accountManager.getAccountsByType("com.getpebble.android");
            if (myAccounts != null && myAccounts.length > 0) {
                updateAccountInfo(myAccounts[0]);
            }
        }
    }

    public void onAccountsUpdated(Account[] accounts) {
        AccountManager accountManager = AccountManager.get((Context) this.mContext.get());
        if (accountManager != null) {
            Account[] myAccounts = accountManager.getAccountsByType("com.getpebble.android");
            if (myAccounts != null && myAccounts.length > 0) {
                final Account myAccount = myAccounts[0];
                new PblAsyncTask() {
                    public boolean doInBackground() {
                        if (UserObserver.this.mContext.get() == null) {
                            return false;
                        }
                        UserObserver.this.updateAccountInfo(myAccount);
                        return true;
                    }

                    public void onTaskSuccess() {
                    }

                    public void onTaskFailed() {
                    }
                }.submit();
            }
        }
    }

    public void updateAccountInfo(Account myAccount) {
        AccountManager accountManager = AccountManager.get((Context) this.mContext.get());
        if (accountManager != null) {
            Map<String, Object> event = AnalyticsLogger.getStaticGlobalProperties();
            AnalyticsLogger.getOrCreateChildMap(event, "identity").put("user", accountManager.getUserData(myAccount, "uid"));
            AnalyticsLogger.getOrCreateChildMap(event, "device_phone").put("name", accountManager.getUserData(myAccount, "email"));
            AnalyticsLogger.setStaticGlobalProperties();
        }
    }

    public void register() {
        Context context = (Context) this.mContext.get();
        if (context != null) {
            AccountManager accountManager = AccountManager.get(context);
            if (accountManager != null) {
                accountManager.addOnAccountsUpdatedListener(this, new Handler(Looper.getMainLooper()), true);
                return;
            }
            return;
        }
        throw new IllegalStateException("context went out of scope, should never happen");
    }

    public void unregister() {
        Context context = (Context) this.mContext.get();
        if (context != null) {
            AccountManager accountManager = AccountManager.get(context);
            if (accountManager != null) {
                accountManager.removeOnAccountsUpdatedListener(this);
            }
        }
    }
}
